<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <script src="jquery-1.11.3.min.js"></script>
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0
        }

        header {
            width: 100%;
            height: 50px;
            line-height: 50px;
            text-align: center;
            background-color: #0f0;
        }

        ul {
            list-style: none;
            margin-top: 10px;
            margin-left: 30px;
        }

        li {
            height: 50px;
            line-height: 50px;
        }

        h4 {
            display: inline;
            font-weight: 400;
        }

        li div:first-child {
            float: left;
        }

        li div:last-child {
            float: right;
            margin-right: 30px;
        }
    </style>
</head>

<body>
    <header>收藏</header>
    <div class="main">
        <ul>
            <li>
                <h4>我的一个道姑朋友</h4>
                <div class="collection">收藏</div>
            </li>
            <li>
                <h4>我的好兄弟</h4>
                <div class="collection">收藏</div>

            </li>
            <li>
                <h4>公子呀</h4>
                <div class="collection">收藏</div>
            </li>
            <li>
                <h4>我的青春</h4>
                <div class="collection">收藏</div>
            </li>
            <li>
                <h4>夜空中最亮的星</h4>
                <div class="collection">收藏</div>
            </li>
        </ul>
        <div class="fav"></div>
    </div>
    <script>
        $(function () {
            //localStorage 类型是字符串
            if (!localStorage.getItem("fav")) {
                localStorage.setItem("fav", "[]");
            }
            $(".collection").click(function () {
                //点击收藏的父元素找对应的h4标签获取内容
                var txt = $(this).parent().find("h4").text();
                //字符串转化为数组
                var favArr = JSON.parse(localStorage.getItem('fav'));
                console.log(favArr)//数组
                //在数组中查找指定值,并返回它的索引值(如果没有找到,则返回-1)
                if ($.inArray(txt, favArr) != -1) {

                    alert("已经收藏过了");

                } else {
                    alert("恭喜您收藏成功");
                    setFav(txt);
                }

            })

            function setFav(txt) {

                var favArr = JSON.parse(localStorage.getItem("fav"));
                // alert(favArr);

                favArr.push(txt);
                console.log(favArr)//数组
                localStorage.setItem('fav', JSON.stringify(favArr));

            }
            var arr = JSON.parse(localStorage.getItem("fav"))
            var str = ''
            for (var i = 0; i < arr.length; i++) {

                str += arr[i] + "<br />"

            }

            $(".fav").html(str);
        })
    </script>
</body>

</html>